import 'package:adaptive_action_sheet/adaptive_action_sheet.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:zhou_po/assets/color.dart';

class OSWebViewPage extends StatefulWidget {
  String url;
  OSWebViewPage({super.key, required this.url});

  @override
  State<OSWebViewPage> createState() => _OSWebViewPageState();
}

class _OSWebViewPageState extends State<OSWebViewPage> {
  late WebViewController controller;
  int progressBar = 0;
  @override
  void initState() {
    controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(const Color(0x00000000))
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) {
            setState(() {
              progressBar = progress;
            });
          },
          onPageStarted: (String url) {},
          onPageFinished: (String url) {},
          onWebResourceError: (WebResourceError error) {},
          onNavigationRequest: (NavigationRequest request) {
            return NavigationDecision.navigate;
          },
        ),
      )
      ..loadRequest(Uri.parse(widget.url));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          onPressed: () {
            Navigator.of(context).pop();
          },
          icon: Icon(Icons.chevron_left_rounded),
        ),
        centerTitle: true,
        title: Text(
          "",
          style: TextStyle(
            color: os_black,
            fontSize: 14,
            fontWeight: FontWeight.normal,
          ),
        ),
        actions: [
          IconButton(
            onPressed: () {
              showAdaptiveActionSheet(
                context: context,
                androidBorderRadius: 30,
                actions: <BottomSheetAction>[
                  BottomSheetAction(
                    title: const Text('复制链接'),
                    onPressed: (context) {
                      Clipboard.setData(ClipboardData(text: widget.url));
                      Navigator.of(context).pop();
                    },
                  ),
                ],
                cancelAction: CancelAction(
                  title: const Text('取消'),
                ),
              );
            },
            icon: Icon(Icons.more_horiz_rounded),
          ),
        ],
        toolbarHeight: 40,
        // backgroundColor: os_color,
      ),
      body: Column(
        children: [
          Row(
            children: [
              AnimatedContainer(
                duration: Duration(milliseconds: 200),
                width: progressBar.toDouble() /
                    100.0 *
                    MediaQuery.of(context).size.width,
                height: progressBar.toDouble() >= 80 ? 0 : 3,
                color: Colors.blueAccent,
              ),
            ],
          ),
          Expanded(child: WebViewWidget(controller: controller)),
        ],
      ),
    );
  }
}
